package weaver.jobs.cese;

import weaver.conn.BatchRecordSet;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;

//物料类型和物料数据同步后，更新到建模中
public class SyncWuliao extends BaseCronJob {
    @Override
    public void execute() {
        AddNewType();

        HashMap<String, String> mp = getWuliaoTypeMap();

        //更新物料的父类型
        UpdateWuiaoParent(mp);

        //同步物料主数据
        AddNewWuliao(mp);
    }

    private HashMap<String, String> getWuliaoTypeMap(){
        HashMap<String, String> mp = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = "select id,plbh from uf_wlpl";
        rs.execute(sql);
        while(rs.next()){
            mp.put(rs.getString("plbh"), rs.getString("id"));
        }
        return mp;
    }

    private void AddNewType(){
        BatchRecordSet batchRecordSet = new BatchRecordSet();
        String insert = "insert into uf_wlpl(srm,plbh,wlpl,pljb,sfxmwl,FORMMODEID,MODEUUID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE)values(?,?,?,?,?,?,?,?,?,?)";
        List<List<Object>> parameters = new ArrayList<List<Object>>();

        int formmodeid = 20003;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String nowDate = fmt.format(calendar.getTime());
        String sql = "select t.* from uf_wuliaotype t left join uf_wlpl w on t.MATERIAL_CLASS_CODE=w.plbh where w.id is null";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        while(rs.next()){
            String MATERIAL_CLASS_CODE = rs.getString("MATERIAL_CLASS_CODE");
            String MATERIAL_CLASS_NAME = rs.getString("MATERIAL_CLASS_NAME");
            String sfxmwl = rs.getString("sfxmwl");//是否项目物料
            RecordSet rs2 = new RecordSet();
            String uuid = UUID.randomUUID().toString();
            // rs2.executeUpdate(insert, new Object[]{"0",MATERIAL_CLASS_CODE,MATERIAL_CLASS_NAME,"1","0","0",formmodeid,uuid,"1","0",nowDate});
            List<Object> alist = new ArrayList<>();
            alist.add("0");
            alist.add(MATERIAL_CLASS_CODE);
            alist.add(MATERIAL_CLASS_NAME);
            alist.add("0");
            alist.add(sfxmwl);
            alist.add(formmodeid);
            alist.add(uuid);
            alist.add("1");
            alist.add("0");
            alist.add(nowDate);
            parameters.add(alist);
        }
        batchRecordSet.executeBatchSql(insert, parameters);

        //添加结束后，更新权限
        sql = "select id from uf_wlpl where MODEDATACREATEDATE='"+nowDate+"'";
        rs.execute(sql);
        while(rs.next()){
            int billed = rs.getInt("id");
            ModeRightInfo info2 = new ModeRightInfo();
            info2.setNewRight(true);
            info2.rebuildModeDataShareByEdit(1, formmodeid, billed);
        }
    }

    private void AddNewWuliao(HashMap<String, String> mp){
    	BaseBean baseBean = new BaseBean();
    	baseBean.writeLog( "--------begin-----------AddNewWuliao" );
        int formmodeid = 20001;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String nowDate = fmt.format(calendar.getTime());
        fmt = new SimpleDateFormat("HH:mm:ss");
        String nowTime = fmt.format(calendar.getTime());

        RecordSet rs = new RecordSet();
        HashMap<String,String>mp2 = new HashMap<>();
        String sql = "select id,dm from uf_dw";
        rs.execute(sql);
        while(rs.next()){
            mp2.put(rs.getString("dm"), rs.getString("id"));
        }

        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        RecordSet rs4 = new RecordSet();
        //插入用sql
        String insert = "insert into UF_WLZSJ(wlbm,wlmc,dw,wlpl,FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME,sxdms,sxcms,modeuuid,sfoay,sfyx)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        //更新用sql
        String update = "update UF_WLZSJ set dw=?,wlmc=?,wlpl=?,sxdms=?,sxcms=?,sfyx=? where wlbm=? and sfyx<>1";
        //获取今日中间表数据的sql
        String getdata = "select material_code,material_name,material_class_code,modeuuid,property_long,property_short,purchase_unit,sfyx from uf_wuliao where modedatamodifydatetime='"+nowDate+"'";
        rs1.execute(getdata);
        while(rs1.next()){
            baseBean.writeLog( "in---------getdata" );
            //获取今日中间表数据
            String material_code = rs1.getString("material_code");
            String material_name = rs1.getString("material_name");
            String material_class_code = rs1.getString("material_class_code");
            String property_long = rs1.getString("property_long");
            String property_short = rs1.getString("property_short");
            String modeuuid = rs1.getString("modeuuid");
            String purchase_unit = rs1.getString("purchase_unit");
            int sfyx = rs1.getInt("sfyx");
            String wlpl = "";
            String dw = "";
            if(mp.containsKey(material_class_code)){
                wlpl = mp.get(material_class_code);
            }
            if(mp2.containsKey(purchase_unit)){
                dw = mp2.get(purchase_unit);
            }

            //判断是否已经存在并且是有效非项目物料
            sql = "select id from UF_WLZSJ where wlbm=? and sfyx !=1";
            rs2.executeQuery(sql,material_code);
            baseBean.writeLog("sql="+sql+","+material_code);
            if(rs2.next()){
                String id = rs2.getString("id");//ID
                rs.writeLog("id="+id);
                //存在当前物料编码的非项目物料的场合，更新该非项目物料
                rs4.executeUpdate(update, new Object[]{dw,material_name,wlpl,property_short,property_long,sfyx,material_code});
            } else {
                //不存在当前物料编码的非项目物料的场合，插入新的非项目物料
                rs3.executeUpdate(insert, new Object[]{material_code,material_name,dw,wlpl,formmodeid,1,0,nowDate,nowTime,property_short,property_long,modeuuid.toString(),0,sfyx});
            }
        }
    }

    private void UpdateWuiaoParent(HashMap<String, String> mp){
        BatchRecordSet batchRecordSet = new BatchRecordSet();
        String update = "update uf_wlpl set sjpl=?,pljb=? where id=?";
        List<List<Object>> parameters = new ArrayList<List<Object>>();

        RecordSet rs = new RecordSet();
        String sql = "select id, plbh from uf_wlpl";
        rs.execute(sql);
        while(rs.next()){
        	String id = rs.getString("id");
            String plbh = rs.getString("plbh");
            int pljb = plbh.length() / 2 - 1;
            String last = "";
            switch (pljb){
                case 0:
                    break;
                case 1:
                    last = plbh.substring(0,2);
                    break;
                case 2:
                    last = plbh.substring(0,4);
                    break;
                case 3:
                    last = plbh.substring(0,6);
                    break;
            }
            if(mp.containsKey(last)){
                String sjpl = mp.get(last);
                List<Object> alist = new ArrayList<>();
                alist.add(sjpl);
                alist.add(pljb);
                alist.add(id);
                parameters.add(alist);
            }
        }
        batchRecordSet.executeBatchSql(update, parameters);
    }
}